/** 切换语言 v1.0.0 **/
var kaLanguage = {
    forDomx: function (root1) {//主程序加载 替换中文
        var clv = kaValue.clv();
        kaLanguage.forDomTypex("placeholder", root1, clv);
        kaLanguage.forDomTypex("title", root1, clv);
        kaLanguage.forDomTypex("value", root1, clv);
        if (root1.children != undefined && root1.children.length > 0) {
            var root1Text = $(root1).contents().filter(function() {return this.nodeType === 3; }).text();
            var html = ``;
            for (var i = 0; root1.children[i] != undefined; i++) {
                html = html + $(root1.children[i]).prop("outerHTML");
                kaLanguage.forDomx(root1.children[i]);
            }
            if (clv[root1Text] != undefined && clv[root1Text] != null && clv[root1Text] != "") {
                $(root1).html(html + clv[root1Text]);
            }
        } else {
            var text = $(root1).text();
            if (clv[text] != undefined && clv[text] != null && clv[text] != "") {
                $(root1).text(clv[text]);
            }
        }
    },
    forDomTypex: function (type, root1, clv) { //元素内 替换中文
        var typeName = $(root1).attr(type);
        if (typeName != undefined && typeName != null && typeName != "") {
            if (clv[typeName] != undefined && clv[typeName] != null && clv[typeName] != "") {
                $(root1).attr(type, clv[typeName]);
            }
        }
    },
    loadingType: function () {//加载语言
        var option = kaValue.getUrl("kaLanguageType");
        option.token = 1;
        option.success = function (re) {
            if (re.code == 200) {
                var kaLanguageType = re.data;
                sessionStorage.setItem("kaLanguageType", JSON.stringify(kaLanguageType));
            }
        }
        ka.ajax(option);
    },
    loadingValue: function (type) {//加载语言对应的值
        var option = kaValue.getUrl("kaLanguageValue");
        option.url = option.url + "/" + type;
        option.token = 1;
        option.success = function (re) {
            if (re.code == 200) {
                var kaLanguageValue = re.data;
                sessionStorage.setItem("kaLanguageValue", JSON.stringify(kaLanguageValue));
            }
        }
        ka.ajax(option);
    },
    renderingOption: function (div, callback) { //渲染登录URL
        const mutationCallback = (mutationsList) => {
            for (let mutation of mutationsList) {
                let type = mutation.type;
                switch (type) {
                    case "childList":
                        kaLanguage.forDomx(mutation.target);
                        break;
                    case "attributes":
                        kaLanguage.forDomx(mutation.target);
                        break;
                    case "subtree":
                        kaLanguage.forDomx(mutation.target);
                        break;
                    default:
                        break;
                }
            }
        };
        let config = {
            attributes: true, //目标节点的属性变化
            childList: true, //目标节点的子节点的新增和删除
            characterData: true, //如果目标节点为characterData节点(一种抽象接口,具体可以为文本节点,注释节点,以及处理指令节点)时,也要观察该节点的文本内容是否发生变化
            subtree: true, //目标节点所有后代节点的attributes、childList、characterData变化
        };
        var observe = new MutationObserver(mutationCallback);
        observe.observe(document, config);// 后面介绍config的配置
        kaLanguage.loadingType();
        var json = JSON.parse(sessionStorage.getItem("kaLanguageType"));
        var html = ``;
        for (var j in json) {
            html = html + `<option ` + (kaValue.clt() == json[j].id ? 'selected' : '') + ` value="` + json[j].id + `">` + json[j].name + `</option>`;
        }
        $("#" + div).html(html);
        if (callback != undefined && callback != null && callback != "") {
            callback();
        }
    }
}